<?php

namespace App\Http\Controllers\Mobile;


use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class SecurityController extends BaseController
{
    public function index()
    {
        $user = Auth::user();
        return $this->view('mobile.user.security', ['user'=>$user]);
    }

    public function setPassword()
    {
        if ($this->isOnSubmit()) {
            $password = $this->request->post('password');
            Auth::user()->update(['password'=>Hash::make($password)]);
            return ajaxReturn();
        } else {
            return $this->view('mobile.user.setpassword');
        }
    }

    public function setEmail()
    {
        if ($this->isOnSubmit()) {
            $email = $this->request->post('email');
            if (User::where('email', $email)->exists()){
                return ajaxError(1, trans('user.email be occupied'));
            }

            Auth::user()->update(['email'=>$email]);
            return ajaxReturn();
        } else {

            return $this->view('mobile.user.setemail');
        }
    }

    public function setMobile()
    {
        if ($this->isOnSubmit()) {
            $mobile = $this->request->post('mobile');
            if (User::where('mobile', $mobile)->exists()){
                return ajaxError(1, trans('user.mobile be occupied'));
            }

            Auth::user()->update(['mobile'=>$mobile]);
            return ajaxReturn();

        } else {

            return $this->view('mobile.user.setmobile');
        }
    }
}
